#include "sagevis.h"

#include "GenoSage/genosage.h"
#include "VisBaseClasses/visbasewidget.h"
#include "GenoSage/MultiGeneRegionVisWidget/multigeneregionviswidget.h"


//********************************CONSTRUCTOR and DESTRUCTOR***************************************//

//SageVis constructor
//  Create the program files directory - where intermediate files can be stored
//  Create timer that controls updating and animation of objects
SageVis::SageVis(QGraphicsItem *parent) :
    QGraphicsWidget(parent)
{
    qDebug() << "SageVis class created" << endl;

    //-------------------------------------------------------------
    //PROGRAM FILES DIRECTORY: Create a directory to store application files
    //  This is where intermediate data files can be stored
    //  It is in the same path as the application itself
    //   which makes it easier to launch a standalone application
    qDebug() << "Creating SageVis program files directory at: " << QApplication::applicationDirPath().append("/programFiles/dataFiles") << endl;
    QDir dir(QApplication::applicationDirPath().append("/programFiles/dataFiles"));
    if (!dir.exists()) {
        dir.mkpath(".");
    }
    //-------------------------------------------------------------
    //KEYPRESS EVENTS: Ensure that SageVis can recieve keypress events
    //  Note: not sure this is needed anymore
    setFlag( QGraphicsItem::ItemIsFocusable );
    //-------------------------------------------------------------
    //CREATE TIMER
    //  Note: this may make timer in main unnecessary
    timer = new QTimer(this);
    timer->setInterval( 100 );
    timer->start();
}

//Destructor
//  Remove widgets and delete timer
SageVis::~SageVis()
{
    //Turn on once widgets added
//    for(int i = 0 ; i < widgets.size(); i++)
//        if( widgets[i] )
//            delete widgets[i];

    delete timer;

    //remove programFiles data directory
    //  need to debug this
    //QDir dir;
    //dir.removeRecursively( QApplication::applicationDirPath().append("/programFiles/dataFiles") );
}
//*****************************END CONSTRUCTOR and DESTRUCTOR***************************************//


//*****************************LAUNCH WIDGET***************************************//
//2 modes for launching a widget- with data path and without

//Launch widget with data path
//      I am leaving this empty because I am not using it yet
void SageVis::launchVisWidget(QString type, QString pathToData)
{
    //empty for now ...
}

//Launch widget without data path
//  If GenoSage not created, create genoSage if it is a GenoSage Application
void SageVis::launchVisWidget( QString type )
{
    qDebug() << "Sage Vis Launching " << type <<  endl;

    type = type.toLower();// make sure lower case, so won't miss calls
    bool successfulWidgetLauch = false;//make sure widget successfully launchess

    //------GENE REGION VIS WIDGET------
    if( type.contains("generegionviswidget") )
    {
        //----If genoSage not already created, create it
        if( !genoSage )
        {
            qDebug() << "Creating GenoSage" << endl;
            genoSage = new GenoSage( this );
        }

        //---- Create MultiGeneRegionVisWidget
        widgets << new MultiGeneRegionVisWidget(genoSage, this, this);//parameters are genoSage, sageVis and parent widget

        int idx = widgets.size()-1; //add MultiGeneRegionVisWidget to GenoSage
        genoSage->addWidget( widgets[idx] );

        successfulWidgetLauch = true;
    }

    //------ENSURE NEW WIDGET IS RECIEVING TIMER EVENTS-----
    if( successfulWidgetLauch )
        connect(timer, SIGNAL(timeout()), widgets.at(widgets.size()-1), SLOT(advance()));
}

//*************************END LAUNCH WIDGET***************************************//


